package org.wattdepot.cli.command;

import java.util.List;
import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.cli.CommandLineInterface;
import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.resource.sensordata.jaxb.SensorData;
import org.wattdepot.util.tstamp.Tstamp;

/**
 * Prints a table listing all of the sensor data instances for the day.
 * 
 * @author Anthony Xu & John Mack
 *
 */
public class ListSensordataDayCommand implements WattDepotCliCommand {

  /**
   * Prints a table listing all of the sensor data instances for the day.
   * 
   * @param cli WattDepotClient with hostUri = http://server.wattdepot.org:8182/wattdepot/
   * @param commands user's command line input
   * @return string to output
   */
  public String doCommand(CommandLineInterface cli, List<String> commands) {
    String ret = "";
    String source = commands.get(1);
    String day = commands.get(3);
    SensorData data = null;
    XMLGregorianCalendar theDay = null;
    try {
      theDay = Tstamp.makeTimestamp(day);
    }
    catch (Exception e) {
      System.err.println("Unable to create timestamp");
    }
    try {
      data = cli.getClient().getSensorData(source, theDay);
    }
    catch (NotAuthorizedException e) {
      System.err.println("Not authorized to view source.");
    }
    catch (ResourceNotFoundException e) {
      System.err.println("Source name not found.");
    }
    catch (BadXmlException e) {
      System.err.println("Server provided bad XML: " + e.toString());
    }
    catch (MiscClientException e) {
      System.err.println("Problem encountered with server: " + e.toString());
    }
    if (data == null) {
      System.err.println("Unable to retrieve sensor data.");
    }
    else {
      ret = ret + data.getProperties();
      ret = ret + data.getTool();
      ret = ret + data.getTimestamp();
    }
    return ret;
  }
  
}
